package com.excel.analysis;

import java.io.File;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.Node;

/**
 * 计算问卷
 * 
 * @author 孙树林
 * 
 */
public class Calculation {

	private XMLHandler xMLHandler = null;
	private File file = null;

	/**
	 * 构造函数
	 * 
	 * @param file
	 * @throws Exception
	 */
	public Calculation(File file) throws Exception {
		this.file = file;
		xMLHandler = new XMLHandler(file);
	}

	/**
	 * 考核项目分数计算
	 * 
	 * @throws Exception
	 */
	public void calulation() throws Exception {
		List<Node> list = xMLHandler.selectNodes("//entry");
		int total_norm_source = 0;
		int total_source = 0;
		for (Node n : list) {
			Element e = (Element) n;
			Attribute norm_score = e.attribute("norm-score");
			Attribute score = e.attribute("score");
			int s = Integer.parseInt(e.attributeValue("source"));
			String rule = e.attributeValue("scoring-rules"); // 扣分规则
			Attribute use = e.attribute("use");
			if(use != null && use.getValue().equals("y")) {
				// y不适用
				norm_score.setValue(String.valueOf(s));
				score.setValue("0");
				total_norm_source += s;
				total_source += 0;
			} else {
				// n适用
				List<Node> ns = xMLHandler.selectNodes(
						"item[@selected='y']", e);
				if (ns == null || ns.size() == 0) {
					norm_score.setValue(String.valueOf(s));
					score.setValue(String.valueOf(s));
					total_norm_source += s;
					total_source += s;
					continue;
				}
				if (rule.equals("1")) {
					for (Node _n : ns) {
						Element _e = (Element) _n;
						int source = Integer.parseInt(_e.attributeValue("source"));
						s = s - source;
					}
					norm_score.setValue(String.valueOf(s));
					score.setValue(String.valueOf(s));
					total_norm_source += s;
					total_source += s;
				}
			}
		}
		// 计算指标得分
		calulationKpi();
		Element e = xMLHandler.getRoot();
		Element cpq = e.element("cpq");
		Attribute cpq_norm = cpq.attribute("norm-score");
		if(cpq_norm == null) {
			cpq.addAttribute("norm-score", String.valueOf(total_norm_source));
		} else {
			cpq_norm.setValue(String.valueOf(total_norm_source));
		}
		Attribute cpq_soucre = cpq.attribute("score");
		if(cpq_soucre == null) {
			cpq.addAttribute("score", String.valueOf(total_source));
		} else {
			cpq_soucre.setValue(String.valueOf(total_source));
		}
		xMLHandler.saveAs(file);
	}
	
	/**
	 * 计算指标得分
	 * 
	 * @throws Exception
	 */
	public void calulationKpi() throws Exception {
		List<Node> list = xMLHandler.selectNodes("//kpi");
		for(Node n : list) {
			Element _n = (Element) n;
			Attribute norm_score = _n.attribute("norm-score");
			Attribute _score = _n.attribute("score");
			int norm = 0;
			int score = 0;
			List<Node> ns = xMLHandler.selectNodes(".//entry/@score", _n);
			for(Node no : ns) {
				Attribute a = (Attribute) no;
				norm = norm + Integer.parseInt(a.getValue());
				score = score + Integer.parseInt(a.getParent().attributeValue("norm-score"));
			}
			_score.setValue(String.valueOf(score));
			norm_score.setValue(String.valueOf(norm));
		}
	}
}
